<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var arr = [5,4,3,2,1];
        for(var i = 0;i<arr.length;i++){
           for(var j = i+1;j<arr.length;j++){
                if(arr[i]>arr[j]){
                    //在每一次交换位置的时候，需要保存前面变量
                    var temp = arr[i]; // 5
                    arr[i] = arr[j]; //[4,4,3,2,1]
                    arr[j] = temp; // [4,5,3,2,1]
                }
           }
        }
        console.log(arr);
    </script>
</body>
</html>